package com.wcj.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class GetUrlContentUtils {

	public static String getContent(String url)
			throws FailingHttpStatusCodeException, MalformedURLException,
			IOException {
		WebClient webClient = new WebClient();
//		webClient.setTimeout( 2000 );
//		webClient.setWebConnection(webConnection);
//		WebConnection d 
		final HtmlPage page = webClient.getPage(url);
		try {
			Thread.sleep(2000);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		String value = page.asXml();
		webClient.closeAllWindows();
		return value;
	}

	public static String getContentByHttpClient(String url , String chaset ) throws Exception {
		URL urlmy = new URL(url);

		HttpURLConnection con = (HttpURLConnection) urlmy.openConnection();

		con.setFollowRedirects(true);
		con.setReadTimeout(10000);
		con.setInstanceFollowRedirects(false);

		con.connect();

		BufferedReader br = new BufferedReader(new InputStreamReader(
				con.getInputStream(), chaset ) );

		String s = "";
		StringBuffer sb = new StringBuffer("");
		sb.append("");

		while ((s = br.readLine()) != null) {
			sb.append(s + "\r\n");
		}

		return sb.toString();
	}
}
